package com.easyctrl.ldy.net;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
 
import com.easyctrl.ldy.util.FileUtil;

import android.content.Context;
import android.util.Log;

public class SocketUtil {

	private Socket socket;

	private String ip;
	private int port;

	private OutputStream out;
	private InputStream in;

	public void connect() throws Exception {

		socket = new Socket();
		SocketAddress address = new InetSocketAddress(ip, port);
		socket.connect(address, 2000);
	}

	private Context context;

	public SocketUtil(String ip, int port, Context context) {
		this.ip = ip;
		this.port = port;
		this.context = context;
		try {
			connect();
		} catch (Exception e) {
			e.printStackTrace();
			Log.i("data", "SocketUtil socket = " + e.getMessage());
		}

		try {
			if (socket != null && !socket.isClosed()) {
				in = socket.getInputStream();
			}
		} catch (IOException e) {
			e.printStackTrace();
			Log.i("data", "SocketUtil socket = " + e.getMessage());
		}

	}

	public void sendData(String order) throws Exception {
		out = socket.getOutputStream();
		out.write(order.getBytes());
	}

	public byte[] readStream(InputStream in) throws IOException {
		int count = 0;

		while (count == 0) {
			count = in.available();
		}
		byte[] buf = new byte[count];
		in.available();
		in.read(buf);
		return buf;
	}

	public void readData(String saveName) {
		byte[] buf = new byte[1024];
		int len = 0;
		try {
			String str = "";

			while ((len = in.read(buf)) != -1) {
				str += new String(buf, 0, len);
			}
			FileUtil.writerData(saveName, str, context);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {

				if (in != null)
					in.close();
				if (socket != null)
					socket.close();

				in = null;
				socket = null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public boolean downDB(String path,String savename){
		boolean isSave = false; // 是否保存 (false 主机没有数据) (true 主机有数据并且保持)

		ByteArrayOutputStream bos = new ByteArrayOutputStream();

		byte[] buffer = new byte[1024];
		int len = 0;
		try {
			while ((len = in.read(buffer)) != -1) {
				bos.write(buffer, 0, len);
			} 
			byte[] data = bos.toByteArray();
			if (data.length == 0) {
				isSave = true;
			} else {
				FileUtil.writeData(data,path,savename);
				isSave = false;
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {

			try {
				if (bos != null) {
					bos.close();
				}
				if (in != null) {
					in.close();
				}
				if (socket != null) {
					socket.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		} 
		return isSave;
	}
	
	public boolean saveDB() {

		boolean isSave = false; // 是否保存 (false 主机没有数据) (true 主机有数据并且保持)

		ByteArrayOutputStream bos = new ByteArrayOutputStream();

		byte[] buffer = new byte[1024];
		int len = 0;
		try {
			while ((len = in.read(buffer)) != -1) {
				bos.write(buffer, 0, len);
			} 
			byte[] data = bos.toByteArray();
			if (data.length == 0) {
				isSave = true;
			} else {
				FileUtil.writeDataDB(data);
				isSave = false;
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {

			try {
				if (bos != null) {
					bos.close();
				}
				if (in != null) {
					in.close();
				}
				if (socket != null) {
					socket.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return isSave;
	}
}
